Completed
Push — master ( 6eced6...eadcde )
by Equim
01:04
created

access.js ➔ ... ➔ superagent.end   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 51
rs 9.4109
c 1
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A end 0 17 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
'use strict';
2
3
var superagent = require('superagent'),
4
    escaper    = require('true-html-escape'),
5
    colors     = require('colors'),
6
    Date       = require('./Date.js');
0 ignored issues
show
Comprehensibility introduced by
You are shadowing the built-in type Date. This makes code hard to read, consider using a different name.
Loading history...
7
8
const timeStamp = () => new Date().format('[MM-dd hh:mm:ss] ');
9
10
// 直接从主页上扒下来的,加密算法
11
const encodeInp = (input) => {
12
    let keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
13
    let output = "";
14
    let chr1, chr2, chr3 = "";
15
    let enc1, enc2, enc3, enc4 = "";
16
    let i = 0;
17
    do {
18
        chr1 = input.charCodeAt(i++);
19
        chr2 = input.charCodeAt(i++);
20
        chr3 = input.charCodeAt(i++);
21
        enc1 = chr1 >> 2;
22
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
23
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
24
        enc4 = chr3 & 63;
25
        if (isNaN(chr2)) {
26
            enc3 = enc4 = 64;
27
        } else if (isNaN(chr3)) {
28
            enc4 = 64;
29
        }
30
        output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
31
        chr1 = chr2 = chr3 = "";
0 ignored issues
show
Unused Code introduced by
The assignment to variable chr1 seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The assignment to variable chr2 seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The assignment to variable chr3 seems to be never used. Consider removing it.
Loading history...
32
        enc1 = enc2 = enc3 = enc4 = "";
0 ignored issues
show
Unused Code introduced by
The assignment to variable enc3 seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The assignment to variable enc4 seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The assignment to variable enc2 seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The assignment to variable enc1 seems to be never used. Consider removing it.
Loading history...
33
    } while (i < input.length);
34
    return output;
35
};
36
37
// 登出时用到的函数,一样是从原网页扒下来的
38
const getRandomUrl = (htmlurl) => {
39
    let count = htmlurl.indexOf("?");
40
    let date = new Date();
41
    let t = Date.parse(date);    
42
    if (count < 0) {
43
        htmlurl = htmlurl + "?tktime=" + t;
44
    } else {
45
        htmlurl = htmlurl + "&tktime=" + t;
46
    }
47
    return htmlurl;
48
};
49
50
// 登录模块
51
const login = (id, pwd, res, callback) => {
52
    // 通过GET首页,来获取cookie
53
    superagent
54
        .get('http://csujwc.its.csu.edu.cn/jsxsd')
55
        .end(function (err, ires) {
56
            if (err) {
57
                console.log((timeStamp() + 'Failed to get the Cookie.\n' + err.stack).red);
1 ignored issue
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
58
                res.send({ error: '获取Cookie失败' });
59
                return;
60
            }
61
62
            // 登录POST请求的headers,是我抓包来的
63
            let headers = {
64
                Host: 'csujwc.its.csu.edu.cn',
65
                Connection: 'keep-alive',
66
                'Cache-Control': 'max-age=0',
67
                Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
68
                Origin: 'http://csujwc.its.csu.edu.cn',
69
                'Upgrade-Insecure-Requests': 1,
70
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
71
                'Content-Type': 'application/x-www-form-urlencoded',
72
                Referer: 'http://csujwc.its.csu.edu.cn/jsxsd/',
73
                'Accept-Encoding': 'gzip, deflate',
74
                'Accept-Language': 'zh-CN,zh;q=0.8',
75
                Cookie: ires.headers['set-cookie']     // 猜测:感觉这个cookie也是不变的,不过出于稳定性,还是动态获取了
76
            };
77
78
            let account = encodeInp(id);
79
            let passwd = encodeInp(pwd);
80
            let encoded = escaper.escape(account + "%%%" + passwd);
81
82
            // POST登录
83
            superagent
84
                .post('http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk')
85
                .set(headers)
86
                .type('form')
87
                .send({ encoded: encoded })
88
                .end(function (err, iires) {
89
                    // 如果登录信息正确,这里是对http://csujwc.its.csu.edu.cn/jsxsd/framework/xsMain.jsp的GET请求
90
                    // 如果错误,会变成对http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk的POST请求
91
                    if (err) {
92
                        console.log((timeStamp() + 'Failed to login\n' + err.stack).red);
1 ignored issue
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
93
                        res.send({ error: '登录失败' });
94
                        return;
95
                    }
96
                    if (/POST/i.test(iires.req.method)) {
97
                        console.log((timeStamp() + 'Failed to login\nPossibily id or password provided were wrong').red);
98
                        res.send({ error: '登录失败,可能是用户名或密码错误,请确认参数已URL转义' });
99
                        return;
100
                    }
101
                    
102
                    // 将相应的headers和返回的response(刚进去的首页)传入callback
103
                    callback(headers, iires);
104
                });
105
        });
106
};
107
108
// 登出模块
109
const logout = (headers, res, callback) => {
110
    superagent
111
        .get(getRandomUrl('http://csujwc.its.csu.edu.cn/jsxsd/xk/LoginToXk?method=exit'))
112
        .set(headers)
113
        .end(function (err, ires) {
0 ignored issues
show
Unused Code introduced by
The parameter ires is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
114
            if (err) {
115
                console.log((timeStamp() + 'Failed to logout\n' + err.stack).red);
1 ignored issue
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
116
                res.send({ error: '登出失败' });
117
                return;
118
            }
119
            callback();
120
        });
121
};
122
123
module.exports.login = login;
124
module.exports.logout = logout;
125